﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace ProjectEdificio
{

    public partial class DefinirExpensas : Window
    {
        private ExpensaGneral expensa = null;
        private BDManagment bdm = null;

        public DefinirExpensas(ExpensaGneral expensa)
        {
            InitializeComponent();
            bdm = BDManagment.getInstance();

            this.expensa = expensa;
            if (expensa != null)
            {
                this.textBoxAguaComun.Text = expensa.AguaComun.ToString();
                this.textBoxLuzComun.Text = expensa.LuzComun.ToString();
                this.textBoxPortero1.Text = expensa.Portero1.ToString();
                this.textBoxPortero2.Text = expensa.Portero2.ToString();
                this.textBoxSereno.Text = expensa.Sereno.ToString();
            }
            else
            {
                this.expensa = new ExpensaGneral(new DateTime(1,1,1));
            }
        }

        private void buttonAceptar_Click(object sender, RoutedEventArgs e)
        {
            if (textBoxAguaComun.Text != string.Empty && textBoxLuzComun.Text != string.Empty && textBoxPortero1.Text != string.Empty && textBoxPortero2.Text != string.Empty && textBoxSereno.Text != string.Empty)
            {
                if (expensa.FechaRedaccion < DateTime.Now)
                {
                    expensa.AguaComun = float.Parse(textBoxAguaComun.Text);
                    expensa.LuzComun = float.Parse(textBoxLuzComun.Text);
                    expensa.Portero1 = float.Parse(textBoxPortero1.Text);
                    expensa.Portero2 = float.Parse(textBoxPortero2.Text);
                    expensa.Sereno = float.Parse(textBoxSereno.Text);

                    if (expensa.FechaRedaccion.Month != DateTime.Now.Month)
                    {
                        expensa.FechaRedaccion = DateTime.Now;
                        bdm.addExpensaGneral(expensa);
                        this.Close();
                    }
                    else
                    {
                        bdm.editExpensaGneral(expensa);
                        this.Close();
                    }
                }
            }
            else
            {
                MessageBox.Show("Complete todos los campos, si no quiere rellenar algun gasto en especial coloque 0");
            }
            
        }

        private void numerosTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key >= Key.D0 && e.Key <= Key.D9 || e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9)
                e.Handled = false;
            else
                e.Handled = true;
        }

        private void buttonCancelar_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
